package com.ljt.business.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljt.business.entity.InStock;
import com.ljt.business.service.InStockService;
import com.ljt.response.Result;
import com.ljt.utils.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/inStock")
@CrossOrigin
public class InStockController {
    @Autowired
    private InStockService inStockService;

    @ApiOperation(value = "入库订单详细列表",notes = "分页查询入库单详细信息")
    @GetMapping("/queryAllInStockByPage")
    public Result queryAllInStockByPage(@RequestParam(required = false,defaultValue = "1") Integer current,
                                        @RequestParam(required = false,defaultValue = "6") Integer size){
        Page<InStock> inStockPage=new Page<>(current,size);
        inStockPage=inStockService.page(inStockPage);
        return Result.ok().data("inStockPage",inStockPage);
    }
    @ApiOperation(value = "入库订单详细列表",notes = "修改入库单信息")
    @GetMapping("/updateInStockById")
    public Result updateInStockById(@RequestBody InStock inStock){
        boolean i=inStockService.updateById(inStock);
        if(i){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
    @ApiOperation(value="入库订单详细列表",notes = "添加入库单信息")
    @GetMapping("/insertInStock")
    public Result insertInStock(@RequestBody InStock inStock){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for (int i=0;i<5;i++){
            result+=random.nextInt(10);
        }
        inStock.setInNum("XG"+newDate+result);
        boolean i=inStockService.save(inStock);
        if(i){
            return Result.ok().data("InStock",inStock);
        }else{
            return Result.error();
        }
    }
    @ApiOperation(value = "导出",produces="application/octet-stream")
    @GetMapping("exportInStockToExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<InStock> inStock = inStockService.list();
        ExcelUtil<InStock> util = new ExcelUtil<>(InStock.class);
        util.exportExcel(response,inStock , "入库单数据");
    }

    @ApiOperation("导入")
    @PostMapping("importInStockToExcel")
    public Result importExcel(@RequestBody MultipartFile file) throws Exception {
        ExcelUtil<InStock> util = new ExcelUtil<>(InStock.class);
        List<InStock> inStock = util.importExcel(file.getInputStream());
        boolean insertFlag = inStockService.saveBatch(inStock);
        if (insertFlag){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
}
